/*
 * Page1.java
 *
 * Created on 09-oct-2008, 16:30:28
 */
package investigaciones_itm;

import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Body;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.Form;
import com.sun.webui.jsf.component.Head;
import com.sun.webui.jsf.component.Html;
import com.sun.webui.jsf.component.Link;
import com.sun.webui.jsf.component.MessageGroup;
import com.sun.webui.jsf.component.Page;
import com.sun.webui.jsf.component.PanelLayout;
import com.sun.webui.jsf.component.RadioButton;
import com.sun.webui.jsf.model.Option;

import com.sun.webui.jsf.model.SingleSelectOptionsList;
import itm.bo.login.LoginBO;
import itm.exception.LoginException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlForm;
import javax.faces.component.html.HtmlInputSecret;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import itm.conexion.*;
import itm.exception.ControlException;
import itm.vo.login.LoginVO;
import itm.vo.login.RolVO;
import java.util.ArrayList;
import java.util.List;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @author Administrador
 */
public class Inicio extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private Page page1 = new Page();

    public Page getPage1() {
        return page1;
    }

    public void setPage1(Page p) {
        this.page1 = p;
    }
    private Html html1 = new Html();

    public Html getHtml1() {
        return html1;
    }

    public void setHtml1(Html h) {
        this.html1 = h;
    }
    private Head head1 = new Head();

    public Head getHead1() {
        return head1;
    }

    public void setHead1(Head h) {
        this.head1 = h;
    }
    private Link link1 = new Link();

    public Link getLink1() {
        return link1;
    }

    public void setLink1(Link l) {
        this.link1 = l;
    }
    private Body body1 = new Body();

    public Body getBody1() {
        return body1;
    }

    public void setBody1(Body b) {
        this.body1 = b;
    }
    private Form form1 = new Form();

    public Form getForm1() {
        return form1;
    }

    public void setForm1(Form f) {
        this.form1 = f;
    }
    private Form form2 = new Form();

    public Form getForm2() {
        return form2;
    }

    public void setForm2(Form f) {
        this.form2 = f;
    }
    private HtmlForm form3 = new HtmlForm();

    public HtmlForm getForm3() {
        return form3;
    }

    public void setForm3(HtmlForm hf) {
        this.form3 = hf;
    }
    private HtmlPanelGrid gridPanel4 = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanel4() {
        return gridPanel4;
    }

    public void setGridPanel4(HtmlPanelGrid hpg) {
        this.gridPanel4 = hpg;
    }
    private HtmlOutputText outputText1 = new HtmlOutputText();

    public HtmlOutputText getOutputText1() {
        return outputText1;
    }

    public void setOutputText1(HtmlOutputText hot) {
        this.outputText1 = hot;
    }
    private HtmlCommandButton button1 = new HtmlCommandButton();

    public HtmlCommandButton getButton1() {
        return button1;
    }

    public void setButton1(HtmlCommandButton hcb) {
        this.button1 = hcb;
    }
    private HtmlPanelGrid panelPrincipal = new HtmlPanelGrid();

    public HtmlPanelGrid getPanelPrincipal() {
        return panelPrincipal;
    }

    public void setPanelPrincipal(HtmlPanelGrid hpg) {
        this.panelPrincipal = hpg;
    }
    private HtmlPanelGrid login = new HtmlPanelGrid();

    public HtmlPanelGrid getLogin() {
        return login;
    }

    public void setLogin(HtmlPanelGrid hpg) {
        this.login = hpg;
    }
    private HtmlPanelGrid info = new HtmlPanelGrid();

    public HtmlPanelGrid getInfo() {
        return info;
    }

    public void setInfo(HtmlPanelGrid hpg) {
        this.info = hpg;
    }
    private HtmlCommandButton textFieldLogin = new HtmlCommandButton();

    public HtmlCommandButton getTextFieldLogin() {
        return textFieldLogin;
    }

    public void setTextFieldLogin(HtmlCommandButton hcb) {
        this.textFieldLogin = hcb;
    }
    private HtmlPanelGrid gridPanel1 = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanel1() {
        return gridPanel1;
    }

    public void setGridPanel1(HtmlPanelGrid hpg) {
        this.gridPanel1 = hpg;
    }
    private HtmlPanelGrid gridPanel2 = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanel2() {
        return gridPanel2;
    }

    public void setGridPanel2(HtmlPanelGrid hpg) {
        this.gridPanel2 = hpg;
    }
    private HtmlOutputText subtitulo1 = new HtmlOutputText();

    public HtmlOutputText getSubtitulo1() {
        return subtitulo1;
    }

    public void setSubtitulo1(HtmlOutputText hot) {
        this.subtitulo1 = hot;
    }
    private HtmlOutputText usr = new HtmlOutputText();

    public HtmlOutputText getUsr() {
        return usr;
    }

    public void setUsr(HtmlOutputText hot) {
        this.usr = hot;
    }
    private HtmlOutputText clave = new HtmlOutputText();

    public HtmlOutputText getClave() {
        return clave;
    }

    public void setClave(HtmlOutputText hot) {
        this.clave = hot;
    }
    private HtmlOutputText subtitulo2 = new HtmlOutputText();

    public HtmlOutputText getSubtitulo2() {
        return subtitulo2;
    }

    public void setSubtitulo2(HtmlOutputText hot) {
        this.subtitulo2 = hot;
    }
    private HtmlPanelGrid gridPanel3 = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanel3() {
        return gridPanel3;
    }

    public void setGridPanel3(HtmlPanelGrid hpg) {
        this.gridPanel3 = hpg;
    }
    private HtmlOutputText subtitulo3 = new HtmlOutputText();

    public HtmlOutputText getSubtitulo3() {
        return subtitulo3;
    }

    public void setSubtitulo3(HtmlOutputText hot) {
        this.subtitulo3 = hot;
    }
    private HtmlPanelGrid gridPanel5 = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanel5() {
        return gridPanel5;
    }

    public void setGridPanel5(HtmlPanelGrid hpg) {
        this.gridPanel5 = hpg;
    }
    private RadioButton radio1 = new RadioButton();

    public RadioButton getRadio1() {
        return radio1;
    }

    public void setRadio1(RadioButton rb) {
        this.radio1 = rb;
    }
    private RadioButton radio3 = new RadioButton();

    public RadioButton getRadio3() {
        return radio3;
    }

    public void setRadio3(RadioButton rb) {
        this.radio3 = rb;
    }
    private RadioButton radio2 = new RadioButton();

    public RadioButton getRadio2() {
        return radio2;
    }

    public void setRadio2(RadioButton rb) {
        this.radio2 = rb;
    }
    private HtmlCommandButton buscar = new HtmlCommandButton();

    public HtmlCommandButton getBuscar() {
        return buscar;
    }

    public void setBuscar(HtmlCommandButton hcb) {
        this.buscar = hcb;
    }
    private PanelLayout layoutPanel1 = new PanelLayout();

    public PanelLayout getLayoutPanel1() {
        return layoutPanel1;
    }

    public void setLayoutPanel1(PanelLayout pl) {
        this.layoutPanel1 = pl;
    }
    private String nombreUsuario = new String();

    public String getNombreUsuario() {
        return nombreUsuario;
    }

    public void setNombreUsuario(String nombreUsuario) {
        this.nombreUsuario = nombreUsuario;
    }
    private String claveUsuario;

    public String getClaveUsuario() {
        return claveUsuario;
    }

    public void setClaveUsuario(String claveUsuario) {
        this.claveUsuario = claveUsuario;
    }
    private HtmlInputText textFieldNombreusuario = new HtmlInputText();

    public HtmlInputText getTextFieldNombreusuario() {
        return textFieldNombreusuario;
    }

    public void setTextFieldNombreusuario(HtmlInputText hit) {
        this.textFieldNombreusuario = hit;
    }
    private HtmlInputSecret secretFieldClaveusuario = new HtmlInputSecret();

    public HtmlInputSecret getSecretFieldClaveusuario() {
        return secretFieldClaveusuario;
    }

    public void setSecretFieldClaveusuario(HtmlInputSecret his) {
        this.secretFieldClaveusuario = his;
    }
    private HtmlPanelGrid gridPanel6 = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanel6() {
        return gridPanel6;
    }

    public void setGridPanel6(HtmlPanelGrid hpg) {
        this.gridPanel6 = hpg;
    }
    private MessageGroup messageGroup1 = new MessageGroup();

    public MessageGroup getMessageGroup1() {
        return messageGroup1;
    }

    public void setMessageGroup1(MessageGroup mg) {
        this.messageGroup1 = mg;
    }

    // </editor-fold>
    HttpSession session;
    private DropDown dropRoles = new DropDown();
    private SingleSelectOptionsList optionRoles = new SingleSelectOptionsList();

    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public Inicio() {
        cargarListaRoles();
    }

    protected void cargarListaRoles() {
        Option[] options;
        loginBO = new LoginBO();
        List<RolVO> lista = new ArrayList<RolVO>();

        try {
            lista = loginBO.listarRoles();
            options = new Option[lista.size()+1];
            options[0] = new Option("0", "--Seleccione un rol--");
            for(int i=1;i<=lista.size();i++){
                options[i] = new Option(lista.get(i-1).getIdRol(), lista.get(i-1).getNombreRol());
            }
            optionRoles.setOptions(options);
        } catch (LoginException ex) {
            Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
        }

    // </editor-fold>
    // Perform application initialization that must complete
    // *after* managed components are initialized
    // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }
    private LoginBO loginBO;
    private LoginVO loginVO;

    public String textFieldLogin_action() throws ControlException {
        // TODO: Process the button click action. Return value is a navigation
        // case name where null will return to the same page.
        try {
            loginBO = new LoginBO();
            loginVO = new LoginVO();

            loginVO = loginBO.consultarUsuario(nombreUsuario, claveUsuario);

            if (!nombreUsuario.equals("")) {
                if (nombreUsuario.equals(loginVO.getNombreusuario()) && claveUsuario.equals(loginVO.getClaveusuario())) {
                    if(loginBO.personaRolExsite(Long.parseLong(loginVO.getIdPersona()), Long.parseLong(dropRoles.getValue().toString())).equals("existe")){
                    getSessionBean1().setLoggedIn(true);
                    getSessionBean1().setLoggedInUserName(loginVO.getNombre());
                    getSession().setAttribute("usuario", loginVO.getNombre());
                    getSessionBean1().setLoggedInUserId(loginVO.getClaveusuario());
                    this.getSession().setAttribute("rol", dropRoles.getValue().toString());
                    return "login";
                    }else{
                        error("El rol no corresponde al usuario ingresado");
                        return null;
                    }
                    //getSessionBean1().setUsuarioLogin(loginVO.getNombre());                    
                } else {
                    error("Usuario o contraseña no valido...");
                    return null;
                }
            } else {
                error("Digite un Usuario y Contraseña...");
                return null;
            }

        } catch (Exception e) {
            e.getStackTrace();
        }
        return null;
    }

    public HttpSession getSession() {
        return (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
    }

    public DropDown getDropRoles() {
        return dropRoles;
    }

    public void setDropRoles(DropDown dropRoles) {
        this.dropRoles = dropRoles;
    }

    public SingleSelectOptionsList getOptionRoles() {
        return optionRoles;
    }

    public void setOptionRoles(SingleSelectOptionsList optionRoles) {
        this.optionRoles = optionRoles;
    }
}